package com.yunchang.springboot.scheduled.amazon.mws.report.datadispose;

import com.yunchang.springboot.models.Shop;
import com.yunchang.springboot.models.mwsreportbean.MwsDailyInventoryHistory;
import com.yunchang.springboot.models.mwsreportbean.MwsDailyInventoryHistoryDO;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.parent.ReportDataDisposeParent;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.reportinterface.IReportDataDispose;
import com.yunchang.springboot.utils.DateUtil;
import com.yunchang.springboot.utils.ToolUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author czw
 * @date 2020/7/28 0028 18:42
 * @description:
 */
@Component
public class EveryDayInventoryRecordsDataDispose extends ReportDataDisposeParent implements IReportDataDispose {

    /**
     * 每日库存记录 将获取到的数据转为实体对象数组
     *
     * @param lists
     * @param shopBrandName
     * @param area
     * @return
     * @throws Exception
     */
    @Override
    public List<Object> reportDataDispose(List<List<String>> lists, String shopBrandName, String area,String sellerId) throws Exception {
        List<String> titleList = lists.get(0);
        Map<String, Object> titleMap = new HashMap<>(titleList.size());
        for (int i = 0; i < titleList.size(); i++) {
            titleMap.put(titleList.get(i), i);
        }
        if (null != area && area.equals(ESPECIALLY_AREA) && !shopBrandName.equals(SPECIAL_SHOP_NAME) && !shopBrandName.equals(SPECIAL_SHOP_NAME_TWO)) {
            int snapshotDateIndex = Integer.valueOf(String.valueOf(titleMap.get("Date")));
//            //获取数据库里面最新的日期
//            String latestDay = mwsDailyInventoryHistoryMapper.getLatestDay(sellerId, area);
//            if (!StringUtil.isBlank(latestDay)) {
//                // 因为获取的时间都是一样的，所以只要拿一个进行比较就行
//                List<String> firstList = lists.get(1);
//                //如果当前获取的数据的时间和数据库时间一致或者早于数据库时间，本次获取数据将跳过
//                if (DateUtil.compateTime(latestDay, DateUtil.ISO8601ToGMT(firstList.get(snapshotDateIndex))) >= 0) {
//                    return null;
//                }
//            }
            int fnskuIndex = Integer.valueOf(String.valueOf(titleMap.get("FNSKU")));
            int skuIndex = Integer.valueOf(String.valueOf(titleMap.get("Merchant SKU")));
            int productNameIndex = Integer.valueOf(String.valueOf(titleMap.get("Title")));
            int quantityIndex = Integer.valueOf(String.valueOf(titleMap.get("Quantity")));
            int fulfillmentCenterIdIndex = Integer.valueOf(String.valueOf(titleMap.get("FC")));
            int detailedDispositionIndex = Integer.valueOf(String.valueOf(titleMap.get("Disposition")));
            int countryIndex = Integer.valueOf(String.valueOf(titleMap.get("Country")));
            List<Object> list = new ArrayList<>(lists.size());
            for (int i = 1; i < lists.size(); i++) {
                MwsDailyInventoryHistoryDO mwsDailyInventoryHistory = new MwsDailyInventoryHistoryDO();
                List<String> stringList = lists.get(i);
                mwsDailyInventoryHistory.setSellerId(sellerId);
                mwsDailyInventoryHistory.setArea(area);
                mwsDailyInventoryHistory.setSnapshotDate(DateUtil.ISO8601ToGMT(stringList.get(snapshotDateIndex)));
                mwsDailyInventoryHistory.setFnsku(stringList.get(fnskuIndex));
                mwsDailyInventoryHistory.setSku(stringList.get(skuIndex));
                mwsDailyInventoryHistory.setProductName(stringList.get(productNameIndex));
                mwsDailyInventoryHistory.setQuantity(ToolUtil.valueOfInteger(stringList.get(quantityIndex)));
                mwsDailyInventoryHistory.setFulfillmentCenterId(stringList.get(fulfillmentCenterIdIndex));
                mwsDailyInventoryHistory.setDetailedDisposition(stringList.get(detailedDispositionIndex));
                mwsDailyInventoryHistory.setCountry(stringList.get(countryIndex));
                mwsDailyInventoryHistory.setCreateTime(DateUtil.getDate());
                mwsDailyInventoryHistory.setUpdateTime(DateUtil.getDate());
                mwsDailyInventoryHistory.setFinanceStatus(1);
                list.add(mwsDailyInventoryHistory);
            }
            return list;
        } else {
            int snapshotDateIndex = Integer.valueOf(String.valueOf(titleMap.get("snapshot-date")));
//            //获取数据库里面最新的日期
//            String latestDay = mwsDailyInventoryHistoryMapper.getLatestDay(sellerId, area);
//            if (!StringUtil.isBlank(latestDay)) {
//                List<String> firstList = lists.get(1);
//                //如果当前获取的数据的时间和数据库时间一致或者早于数据库时间，本次获取数据将跳过
//                if (DateUtil.compateTime(latestDay, DateUtil.ISO8601ToGMT(firstList.get(snapshotDateIndex))) >= 0) {
//                    return null;
//                }
//            }
            int fnskuIndex = Integer.valueOf(String.valueOf(titleMap.get("fnsku")));
            int skuIndex = Integer.valueOf(String.valueOf(titleMap.get("sku")));
            int productNameIndex = Integer.valueOf(String.valueOf(titleMap.get("product-name")));
            int quantityIndex = Integer.valueOf(String.valueOf(titleMap.get("quantity")));
            int fulfillmentCenterIdIndex = Integer.valueOf(String.valueOf(titleMap.get("fulfillment-center-id")));
            int detailedDispositionIndex = Integer.valueOf(String.valueOf(titleMap.get("detailed-disposition")));
            int countryIndex = Integer.valueOf(String.valueOf(titleMap.get("country")));
            List<Object> list = new ArrayList<>(lists.size());
            for (int i = 1; i < lists.size(); i++) {
                MwsDailyInventoryHistory mwsDailyInventoryHistory = new MwsDailyInventoryHistory();
                List<String> stringList = lists.get(i);
                mwsDailyInventoryHistory.setSellerId(sellerId);
                mwsDailyInventoryHistory.setArea(area);
                mwsDailyInventoryHistory.setSnapshotDate(DateUtil.ISO8601ToGMT(stringList.get(snapshotDateIndex)));
                mwsDailyInventoryHistory.setFnsku(stringList.get(fnskuIndex));
                mwsDailyInventoryHistory.setSku(stringList.get(skuIndex));
                mwsDailyInventoryHistory.setProductName(stringList.get(productNameIndex));
                mwsDailyInventoryHistory.setQuantity(ToolUtil.valueOfInteger(stringList.get(quantityIndex)));
                mwsDailyInventoryHistory.setFulfillmentCenterId(stringList.get(fulfillmentCenterIdIndex));
                mwsDailyInventoryHistory.setDetailedDisposition(stringList.get(detailedDispositionIndex));
                mwsDailyInventoryHistory.setCountry(stringList.get(countryIndex));
                mwsDailyInventoryHistory.setCreateTime(DateUtil.getDate());
                mwsDailyInventoryHistory.setUpdateTime(DateUtil.getDate());
                mwsDailyInventoryHistory.setFinanceStatus(1);
                list.add(mwsDailyInventoryHistory);
            }
            return list;
        }
    }
}
